package com.sttk.secreconeclick.bookkeep.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sttk.secreconeclick.bookkeep.BaseNoModelFragment;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.activity.SaveDetailActivity;
import com.sttk.secreconeclick.bookkeep.adapter.PlanAdapter;
import com.sttk.secreconeclick.bookkeep.bean.PlanBean;
import com.sttk.secreconeclick.bookkeep.databinding.FragmentSaveMoneyBinding;
import com.sttk.secreconeclick.bookkeep.dialog.PlanDialog;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SaveMoneyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SaveMoneyFragment extends BaseNoModelFragment<FragmentSaveMoneyBinding> implements View.OnClickListener, OnItemClickListener {

    private PlanAdapter planAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_save_money;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAdd.setOnClickListener(this);

        mDataBinding.rvPlanList.setLayoutManager(new LinearLayoutManager(mContext));
        planAdapter = new PlanAdapter();
        mDataBinding.rvPlanList.setAdapter(planAdapter);
        planAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivAdd) {//计划
            showPlanDialog();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        SaveDetailActivity.selPosition = position;
        startActivity(SaveDetailActivity.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getData();
        }
    }

    private void getData() {
        int inProgress = 0, inPlan = 0;
        List<PlanBean> listSp = AppSpUtils.getPlanList();
        if (listSp != null && listSp.size() > 0) {
            planAdapter.setList(listSp);
            mDataBinding.llPlan.setVisibility(View.VISIBLE);
            mDataBinding.tvPlanNoData.setVisibility(View.GONE);
            for (PlanBean planBean : listSp) {
                long date = TimeUtils.getNowMills();
                if (date >= planBean.getStartDate()) {
                    inProgress++;
                } else {
                    inPlan++;
                }
            }
        } else {
            mDataBinding.llPlan.setVisibility(View.GONE);
            mDataBinding.tvPlanNoData.setVisibility(View.VISIBLE);
        }
        mDataBinding.tvProgress.setText(String.valueOf(inProgress));
        mDataBinding.tvPlan.setText(String.valueOf(inPlan));
        mDataBinding.tvPlanSize.setText(getString(R.string.plan_size_num, inProgress, inPlan));
    }

    private void showPlanDialog() {
        PlanDialog dialog = new PlanDialog(mContext);
        dialog.setListener(new PlanDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                getData();
            }
        });
        dialog.show();
    }

}